package com.org.any.autocar;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import com.autocar.reader.R;
import com.autocar.reader.util.PreferencesUtil;

public class SettingFontSizeActivity extends BaseActivity implements
		OnClickListener {

	private ImageView backIv;
	private ImageView smallIv;
	private ImageView middleIv;
	private ImageView bigIv;
	private ImageView sobigIv;

	private int rId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_font_change);
		initview();
		initproperty();
	}

	private void initproperty() {
		int font = PreferencesUtil.getFontSize(getApplicationContext());
		if (font == 24) {
			rId = R.id.setting_font_sobig;
		} else if (font == 20) {
			rId = R.id.setting_font_big;
		} else if (font == 16) {
			rId = R.id.setting_font_middle;
		} else {
			rId = R.id.setting_font_small;
		}
		switch (rId) {
		case R.id.setting_font_small:
			this.smallIv.setImageResource(R.drawable.login_checkbox_on);
			break;
		case R.id.setting_font_middle:
			this.middleIv.setImageResource(R.drawable.login_checkbox_on);
			break;
		case R.id.setting_font_big:
			this.bigIv.setImageResource(R.drawable.login_checkbox_on);
			break;
		case R.id.setting_font_sobig:
			this.sobigIv.setImageResource(R.drawable.login_checkbox_on);
			break;
		}

	}

	private void initview() {
		this.backIv = (ImageView) findViewById(R.id.setting_font_top_iv_back);
		this.smallIv = (ImageView) findViewById(R.id.setting_font_small);
		this.middleIv = (ImageView) findViewById(R.id.setting_font_middle);
		this.bigIv = (ImageView) findViewById(R.id.setting_font_big);
		this.sobigIv = (ImageView) findViewById(R.id.setting_font_sobig);

		this.backIv.setOnClickListener(this);
		this.sobigIv.setOnClickListener(this);
		this.bigIv.setOnClickListener(this);
		this.middleIv.setOnClickListener(this);
		this.smallIv.setOnClickListener(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == rId) {
			return;
		}
		int size = -1;
		switch (rId) {
		case R.id.setting_font_small:
			this.smallIv.setImageResource(R.drawable.login_checkbox_off);
			break;
		case R.id.setting_font_middle:
			this.middleIv.setImageResource(R.drawable.login_checkbox_off);
			break;
		case R.id.setting_font_big:
			this.bigIv.setImageResource(R.drawable.login_checkbox_off);
			break;
		case R.id.setting_font_sobig:
			this.sobigIv.setImageResource(R.drawable.login_checkbox_off);
			break;
		}
		switch (v.getId()) {
		case R.id.setting_font_top_iv_back:
			finish();
			break;
		case R.id.setting_font_small:
			size = 12;
			this.smallIv.setImageResource(R.drawable.login_checkbox_on);
			break;
		case R.id.setting_font_middle:
			size = 16;
			this.middleIv.setImageResource(R.drawable.login_checkbox_on);
			break;
		case R.id.setting_font_big:
			size = 20;
			this.bigIv.setImageResource(R.drawable.login_checkbox_on);
			break;
		case R.id.setting_font_sobig:
			size = 24;
			this.sobigIv.setImageResource(R.drawable.login_checkbox_on);
			break;
		}
		rId = v.getId();
		if (size != -1) {
			PreferencesUtil.saveFontSize(getApplicationContext(), size);
		}
	}
	
	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.fade_in, R.anim.right_out_anim);
	}


}
